#!/bin/bash

REPOS_URL="https://gitee.com/wx-rdc/think"

BASE_PATH="$HOME/.docker"

NETWORK_NAME="secloud-network"
MYSQL_NAME="sec-mysql"
REDIS_NAME="sec-redis"

SERVICE_NAME="think"
SERVICE_VERSION="1.0.0"

# registry path
REGISTRY="svr01:5000"

SQL_FILES="think-ddl.sql"

# docker exec -i $MYSQL_NAME mysql -uroot -p123456 < ./sql/think-ddl.sql

importSQL() {
  if [ ! -f $BASE_PATH/sql/$1 ]; then
    wget -O "$BASE_PATH/sql/$1" "$REPOS_URL/raw/master/sql/$1"
    docker exec -i $MYSQL_NAME mysql -uroot -p123456 < $BASE_PATH/sql/$1
  fi
}

if [ ! -d $BASE_PATH ]; then
  mkdir -p $BASE_PATH
  mkdir -p $BASE_PATH/sql
fi

for sqlfile in $SQL_FILES; do
  importSQL $sqlfile
done

docker run -d \
    -p 5001-5003:5001-5003 \
    --restart=on-failure:5 \
    --name think \
    --network $NETWORK_NAME \
    $REGISTRY/$SERVICE_NAME:$SERVICE_VERSION
